package code.rocky.chapter03;

public class Suggest47 {
    public static void main(String[] args) {
            E2 e1 = new E2("Rocky编程日记",100);
            E2 e2 = new E2("Rocky编程日记",1001);
            E1 e3 = new E1("Rocky编程日记");
            System.out.println(e3.equals(e1));
            System.out.println(e3.equals(e2));
            System.out.println(e1.equals(e2));
    }
}

class E2 extends E1 {
    private int id;

    public E2(String _name, int _id) {
        super(_name);
        id = _id;
    }

    public boolean equals(Object obj) {
        if (obj instanceof E2) {
            E2 e = (E2) obj;
            return super.equals(obj) && e.getId() == id;
        }
        return false;
    }

    private int getId() {
        return id;
    }
}

class E1 {

    private String name;

    public E1(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        // 推荐使用 getClass()
        if (obj != null && obj.getClass() == this.getClass()) {
//        if (obj instanceof E1) {
            E1 d = (E1) obj;
            if (d.getName() == null || name == null) {
                return false;
            }
            return name.equalsIgnoreCase(d.getName());
        }
        return false;
    }
    // 使用需引包
//    @Override
//    public int hashCode() {
//        return new HashCodeBuilder().append(name).toHashCode();
//    }



    private String getName() {
        return name;
    }
}
